﻿#include "Date.h"

//全缺省构造函数
Date::Date(int year , int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckDate())
	{
		cout<<"日期非法!"<<endl;
	}

}

//拷贝构造函数
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	if (!CheckDate())
	{
		cout << "日期非法!" << endl;
	}
}

//析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day) const
{
	Date tmp(*this);
	tmp += day;
	return tmp;

}


Date Date::operator-(int day) const
{
	Date tmp(*this);
	tmp -= day;
	return tmp;

}

// 日期-=天数

Date& Date::operator-=(int day)
{

	
	_day -= day;
	while (_day <= 0)
	{
		if (_month == 1)
		{
			_month = 13;
			_year--;
		}
		_day += GetMonthDay(_year, --_month);
	}
	return *this;
}

// 前置++

Date& Date::operator++()
{
	*this += 1;

	return *this;
}


//
// 后置++

Date Date::operator++(int)
{
	Date tmp = *this;
	++*this;
	return tmp;
}



// 后置--

Date Date::operator--(int)
{

	Date tmp = *this;
	--*this;
	return tmp;

}



//// 前置--

Date& Date::operator--()
{
	*this -= 1;
	return *this;

}



// >运算符重载

bool Date::operator>(const Date& d) const

{
	if (_year > d._year)
	{
		return true;
	}
	if (_year == d._year && _month > d._month)
	{
		return true;
	}
	if (_year == d._year && _month == d._month)
	{
		return _day > d._day;
	}
	return false;
}


// ==运算符重载

bool Date::operator==(const Date& d) const
{
	if (_year == d._year && _month == d._month && _day == d._day)
	{
		return true;
	}
	return false;
}



//// >=运算符重载
bool Date::operator >= (const Date& d) const
{
	
	return (*this == d) || (*this > d);

}





//// <运算符重载

bool Date::operator < (const Date& d) const
{
	if (_year < d._year)
	{
		return true;
	}
	if (_year == d._year && _month < d._month)
	{
		return true;
	}
	if (_year == d._year && _month == d._month)
	{
		return _day < d._day;
	}
	return false;
}


// <=运算符重载


bool Date::operator <= (const Date& d) const
{

	return (*this == d) || (*this < d);

}



//// !=运算符重载

bool Date::operator != (const Date& d) const
{
	return !(*this == d);

}
// 日期-日期 返回天数

int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (max != min)
	{
		++min;
		n++;
	}
	return n * flag;
}

ostream& operator<<(ostream& out, const Date& d)
{

	out << d._year <<"年" << d._month <<"月"<< d._day<<"日";
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while(1)
	{
		cout << "请输入日期>";
		in >> d._year >> d._month >> d._day;
		if (!d.CheckDate())
		{
			cout << "日期非法!" << endl;
			continue;
		}
		break;
	}
	
	return in;

}
